﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace BTVPodcatcherNamespace
{
    public partial class DownloadInfoControl : UserControl
    {
        private DowloadInfo downloadControl = null;
        bool ready = false;
        private MainForm MF = null;

        public DowloadInfo downloadJob
        {
            get { return downloadControl; }
        }
        
        public DownloadInfoControl(DowloadInfo di, MainForm mf)
        {
            downloadControl = di;
            MF = mf;
            downloadControl.DownloadChanged += new DowloadInfo.DownloadChangedHandler(downloadControl_DownloadChanged);
            InitializeComponent();
            setControlsData();
            ready = true;
        }

        void downloadControl_DownloadChanged(object myObject, EventArgs myArgs)
        {
            if(ready)
                setControlsData();
        }
        delegate void SetTBFeedTitleTextDelegate(string parameter);
        void SetTBFeedTitleText(string text)
        {
            if (!InvokeRequired)
                TBFeedTitle.Text = text;
            else
                Invoke(new SetTBFeedTitleTextDelegate(SetTBFeedTitleText), new object[] { text });
        }

        delegate void SetTBFeedItemTitleTextDelegate(string parameter);
        void SetTBFeedItemTitleText(string text)
        {
            if (!InvokeRequired)
                TBFeedItemTitle.Text = text;
            else
                Invoke(new SetTBFeedItemTitleTextDelegate(SetTBFeedItemTitleText), new object[] { text });
        }
        delegate void SetTBSpeedTextDelegate(string parameter);
        void SetTBSpeedText(string text)
        {
            if (!InvokeRequired)
                TBSpeed.Text = text;
            else
                Invoke(new SetTBSpeedTextDelegate(SetTBSpeedText), new object[] { text });
        }
        delegate void SetTBURLTextDelegate(string parameter);
        void SetTBURLText(string text)
        {
            if (!InvokeRequired)
                TBURL.Text = text;
            else
                Invoke(new SetTBURLTextDelegate(SetTBURLText), new object[] { text });
        }
        delegate void SetTBFeedItemDescriptionTextDelegate(string parameter);
        void SetTBFeedItemDescriptionText(string text)
        {
            if (!InvokeRequired)
                TBFeedItemDescription.Text = text;
            else
                Invoke(new SetTBFeedItemDescriptionTextDelegate(SetTBFeedItemDescriptionText), new object[] { text });
        }
        delegate void SetLPercentTextDelegate(string parameter);
        void SetLPercentText(string text)
        {
            if (!InvokeRequired)
                LPercent.Text = text;
            else
                Invoke(new SetLPercentTextDelegate(SetLPercentText), new object[] { text });
        }

        delegate void SetprogressBar1ValueDelegate(int parameter);
        void SetprogressBar1Value(int value)
        {
            if (!InvokeRequired)
                progressBar1.Value = value;
            else
                Invoke(new SetprogressBar1ValueDelegate(SetprogressBar1Value), new object[] { value });
        }
        delegate void SetTBStatusTextDelegate(string parameter);
        void SetTBStatusText(string text)
        {
            if (!InvokeRequired)
                TBStatus.Text = text;
            else
                Invoke(new SetTBStatusTextDelegate(SetTBStatusText), new object[] { text });
        }

        delegate void Setbutton1NotVisableDelegate();
        void Setbutton1NotVisable()
        {
            if (!InvokeRequired)
                button1.Visible = false;
            else
                Invoke(new Setbutton1NotVisableDelegate(Setbutton1NotVisable));
        }
        delegate void Setbutton1BTVPropertiesDelegate();
        void Setbutton1BTVProperties()
        {
            if (!InvokeRequired)
            {
                button1.Visible = true;
                button1.Image = BTVPodcatcherNamespace.Properties.Resources.pencil;
                button1.Text = "Write BTV Properties";
            }
            else
                Invoke(new Setbutton1BTVPropertiesDelegate(Setbutton1BTVProperties));
        }
        delegate void Setbutton1BTVPlayDelegate();
        void Setbutton1BTVPlay()
        {
            if (!InvokeRequired)
            {
                button1.Visible = true;
                button1.Image = BTVPodcatcherNamespace.Properties.Resources.Play_icon;
                button1.Text = "Play File          ";
            }
            else
                Invoke(new Setbutton1BTVPlayDelegate(Setbutton1BTVPlay));
        } 
        private void setControlsData()
        {
            if (downloadControl != null)
            {
                SetTBFeedTitleText( downloadControl.rssFeedTitle);
                SetTBFeedItemTitleText( downloadControl.rssFeedItemTitle);
                SetTBSpeedText( string.Format("{0:#,###,###} kBps", ((downloadControl.Speed) / 1024.0)));
                SetTBURLText( downloadControl.DownloadUri.ToString());
                SetTBFeedItemDescriptionText( StringFilter.FilterMarkupTags(true, downloadControl.RSSItem.description.Replace("\r", "").Replace("\n", "")));
                if (downloadControl.Progress >= 0)
                {
                    SetprogressBar1Value(downloadControl.Progress);
                    SetLPercentText( "" + downloadControl.Progress + "%");
                }
                if (downloadControl.DownloadStatus.Equals(DownloadStatus.CancelRequested))
                {
                    SetTBStatusText( "Cancel Requested " + downloadControl.Message);
                    
                }
                else if (downloadControl.DownloadStatus.Equals(DownloadStatus.PropertiesWritten))
                {
                    SetTBStatusText( "BTV Properties Written " + downloadControl.Message);
                    Setbutton1BTVPlay();
                    
                }
                else if (downloadControl.DownloadStatus.Equals(DownloadStatus.WaitingToWriteProperties))
                {
                    SetTBStatusText( "Waiting to Write BTV Properties " + downloadControl.Message);
                    
                }
                else if (downloadControl.DownloadStatus.Equals(DownloadStatus.FailedToWriteProperties))
                {
                    SetTBStatusText( "Failed to Write BTV Properties " + downloadControl.Message);
                    Setbutton1BTVProperties();
                }
                else
                {

                    SetTBStatusText("" + downloadControl.DownloadStatus + " " + downloadControl.Message);
                    if (downloadControl.DownloadStatus == DownloadStatus.Complete)
                    {
                        Setbutton1NotVisable();
                    }
                }
                

            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (button1.Text == "Write BTV Properties")
            {
                downloadControl.DownloadStatus = DownloadStatus.WaitingToWriteProperties;
                BTVWriter btw = new BTVWriter(downloadControl, MF);

                System.Threading.Thread btvWriter = new System.Threading.Thread(new System.Threading.ThreadStart(btw.writeBTVProprties));
                btvWriter.Name = "BTV Podcatcher Media Tag Writer";
                btvWriter.IsBackground = true;
                btvWriter.Start();
                
            }
            else if (button1.Text == "Play File          ")
            {
                playinBTV();
            }
            else
            {
                if (downloadControl.DownloadStatus == DownloadStatus.Downloading || downloadControl.DownloadStatus == DownloadStatus.Ready)
                    downloadControl.DownloadStatus = DownloadStatus.CancelRequested;

            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            downloadControl.DownloadStatus = DownloadStatus.CancelRequested;
            MF.allDownloadingItems.Remove(downloadControl);
            MF.downloadRemoved();
        }
        private void playinBTV()
        {
            string host = ((MainForm)this.ParentForm.MdiParent).BTVServer;
            string port = ((MainForm)this.ParentForm.MdiParent).BTVPort;
            if(host=="127.0.0.1"||host=="localhost")
            {
                host="local";
                port="local";
            }
            File.Delete("temp.ssfp");
            FileStream fs = new FileStream("temp.ssfp",FileMode.CreateNew,FileAccess.Write);
            StreamWriter sw = new StreamWriter(fs);
            sw.WriteLine("<ss-fileplayback-request>");
            sw.WriteLine("<filename>"+downloadControl.Destination.ToString()+"</filename>");
            sw.WriteLine("<position>0</position>");

            sw.WriteLine("<clipstart>-1</clipstart>");
            sw.WriteLine("<clipend>-1</clipend>");
            sw.WriteLine("<clipstate>False</clipstate>");
            sw.WriteLine("<host>"+host + "</host>");
            sw.WriteLine("<port>"+port +"</port>");
            sw.WriteLine("</ss-fileplayback-request>");
            sw.Flush();
            sw.Close();
            System.Diagnostics.Process Proc = new System.Diagnostics.Process();
            
            Proc.StartInfo.FileName = "temp.ssfp";
            Proc.Start();
            
            
        }

        
    }
}
